package com.firemoss.atonement.entitymanager.util
{
	import com.firemoss.atonement.entitymanager.EntityManager;
	import com.firemoss.atonement.mapper.Mapping;
	import com.firemoss.atonement.mapper.resolution.IValueResolver;
	import com.firemoss.atonement.metadata.Property;
	
	import flash.utils.Dictionary;
	
	/**
	 * Commonly-needed resolver for assemblers:  returns an entity for a given class and id.
	 */
	public class EntityIdResolver implements IValueResolver
	{
		public var entityManagerName : String = "";
		public var entityClass : Class
		public var createMissingEntities : Boolean = true
		
		public function EntityIdResolver()
		{
		}
		
		public function resolve(source:*, targetProperty:Property, mapping:Mapping, mappingChain:Dictionary):*
		{
			var entityManager : EntityManager = EntityManager.getEntityManager( entityManagerName );
			var inst : Object =  EntityManager.getEntityManager( entityManagerName ).get( targetProperty.type.clazz, source );
			
			if ( !inst && createMissingEntities )
			{
				inst = new targetProperty.type.clazz()
					
				IdUtil.setIdValue( inst, source, entityManager )
			}
			
			return inst;
		}
	}
}